package cn.yunhe.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;

import cn.yunhe.dao.DeptDao;
import cn.yunhe.entity.Dept;
import cn.yunhe.entity.Emp;
import cn.yunhe.service.DeptService;
import cn.yunhe.service.EmpService;

@Controller
public class DeptController {
	@Autowired
	private DeptService deptService;

	/*
	 * @RequestMapping("/login") public ModelAndView findDrugAll(){ ModelAndView
	 * mav = new ModelAndView("success");
	 * 
	 * return mav; }
	 */
	@RequestMapping("/chaDept")
	public String chaDept1(HttpServletRequest request,HttpServletResponse response) {
		System.out.println("========================");
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		DeptService dept = (DeptService) ac.getBean("deptService");
		HttpSession session = request.getSession();
		Emp emp = (Emp) session.getAttribute("CURR_USER");
		System.out.println("用户等级:" + emp.getElev());
		if (emp.getElev() == 2) {
			List<Dept> list = dept.chaDept();
			for (Dept dept2 : list) {
				System.out.println(dept2.getDename());
			}
			request.setAttribute("list", list);
			return "chaDept";
		} else {
			request.setAttribute("msg", "权限不足，系统检测到你不是管理员，不能执行此操作!!!");
			return "success";
		}

	}

	@RequestMapping("/deidChaDapt")
	public String idchaDept(HttpServletRequest request, HttpServletResponse response, int deid) {
		System.out.println("========================");
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		DeptDao dept = (DeptDao) ac.getBean("deptDao");
		Dept dept2 = dept.idchaDept(deid);

		System.out.println(dept2.getDename());
		request.setAttribute("dept", dept2);
		return "gaiDept";
	}

	@RequestMapping("/gaiDept")
	public String gaiDept(String dename, String deid, String destate, HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("========================");
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		DeptService gai = (DeptService) ac.getBean("deptService");
		Dept dept = new Dept();
		int deid1 = Integer.parseInt(deid);
		int destate1 = Integer.parseInt(destate);
		System.out.println(deid1);
		System.out.println(destate1);
		System.out.println(dename);
		dept.setDeid(deid1);
		dept.setDename(dename);
		dept.setDestate(destate1);
		int num = gai.gaiDept(dept);
		System.out.println(num);
		chaDept1(request, response);
		return "chaDept";
	}
	
	@RequestMapping("/delUpdateDept")
	public String delgaiDept(int deid,HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("========================");
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		DeptService gai = (DeptService) ac.getBean("deptService");
		Dept dept = new Dept();
		
		dept.setDeid(deid);
		
		int num = gai.delUpdateDept(deid);
		System.out.println(num);
		chaDept1(request, response);
		return "chaDept";
	}

	@RequestMapping("/addDept")
	public String addDept(String dename, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("========================");
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		DeptService add = (DeptService) ac.getBean("deptService");
		Dept dept = new Dept();
		dept.setDename(dename);
		System.out.println(dename);
		// int num=add.addDept(new Dept("神经科"));
		int num = add.addDept(dept);
		System.out.println(num);
		chaDept1(request, response);
		return "chaDept";

	}

	@RequestMapping("/delDept")
	public String delDept(int deid, HttpServletRequest request, HttpServletResponse response) {
		System.out.println("========================");
		ApplicationContext ac = new ClassPathXmlApplicationContext("Spring-Context.xml");
		DeptService del = (DeptService) ac.getBean("deptService");
		Dept dept = new Dept();
		dept.setDeid(deid);
		int num = del.delDept(dept);
		System.out.println(num);
		chaDept1(request, response);
		return "chaDept";
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

}
